﻿using AutoMapper;
using Domain.Material_Management;
using Domain.Process_Procedures;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Dto.Material_Management.OutPut;
using ReturnValue;
using System;

namespace MES.Server.API.Application.Command.Material_Management
{
    public class GetProductMaterialsHander : IRequestHandler<GetProductMaterialsCommand, ApiPaging<GetProductMaterialsOutPut>>
    {
        private readonly IRepository<MaterialGroupModel> materialGroupRepo;
        private readonly IRepository<ProcessFlowModel> processFlowRepo;
        private readonly IRepository<ProductMaterialsModel> productMaterialsRepo;
        private readonly IMapper mapper;
        public GetProductMaterialsHander(IRepository<MaterialGroupModel> materialGroupRepo, IRepository<ProcessFlowModel> processFlowRepo, IMapper mapper, IRepository<ProductMaterialsModel> productMaterialsRepo)
        {
            this.materialGroupRepo = materialGroupRepo;
            this.processFlowRepo = processFlowRepo;
            this.mapper = mapper;
            this.productMaterialsRepo = productMaterialsRepo;
        }
        /// <summary>
        /// 产品物料查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiPaging<GetProductMaterialsOutPut>> Handle(GetProductMaterialsCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiPaging<GetProductMaterialsOutPut>();
            var productMaterials = productMaterialsRepo.GetAll().Where(x => x.IsDel == false);
            var materialGroup = materialGroupRepo.GetAll().Where(x => x.IsDel == false);
            var processFlow = processFlowRepo.GetAll().Where(x => x.IsDel == false);
            var linq = from a in productMaterials
                       join b in materialGroup on a.MaterialGroupId equals b.Id
                       join c in processFlow on a.ProcessFlowId equals c.Id into ac
                       from c in ac.DefaultIfEmpty()
                       select new GetProductMaterialsOutPut
                       {
                            Id=a.Id,
                            ProductMaterialsCode=a.ProductMaterialsCode,
                            ProductMaterialsName=a.ProductMaterialsName,
                            ProductMaterialsType=a.ProductMaterialsType,
                            ProductMaterialsSpecs=a.ProductMaterialsSpecs,
                            ProductMaterialsUnit=a.ProductMaterialsUnit,
                            ProductMaterialsforewarn=a.ProductMaterialsforewarn,
                            ProductMaterialsDesc=a.ProductMaterialsDesc,
                            MaterialGroupId=b.Id,
                            MaterialGroupName=b.MaterialGroupName,
                           ProcessFlowId = c.Id,
                           FlowName = c.FlowName,
                           CreaterPeople =a.CreaterPeople,
                            CreateTime=a.CreateTime,
                           UpdateTime = a.UpdateTime ?? DateTime.Now,
                           ProductMaterialsCount = a.ProductMaterialsCount
    };
            result.TotalPage = linq.OrderByDescending(x=>x.CreateTime).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            result.Totalcount  = linq.Count();
            result.Code = StatusCode.Success;
            return Task.FromResult(result);
        }
    }
}
